package org.example.springbootserver.controller;

import lombok.RequiredArgsConstructor;
import org.eclipse.angus.mail.util.MailSSLSocketFactory;
import org.example.springbootserver.pojo.R;
import org.example.springbootserver.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.GeneralSecurityException;
import java.util.concurrent.TimeUnit;

@RequestMapping("/Common")
@RestController
@RequiredArgsConstructor
public class CommonController{

    @Value("${spring.mail.username}")
    private String selfMail;
    private final EmailUtil emailUtil;
    // Redis
    private final RedisTemplate<String, String> redisTemplate;

    @GetMapping("/getEmailVerificationCode")
    public R sendEmail(@RequestParam String emailAddress) {
        // 邮件主题
        String emailSubject = "注册账号-表情图库";
        // 生成验证码
        String verificationCode = emailUtil.generateCode();
        // 构建邮件内容
        String content = "【表情图库】注册验证码："+verificationCode+"。5分钟内有效，请勿泄露。";
        emailUtil.sendEmail(selfMail, emailAddress, emailSubject, content);

        // 将传入邮箱地址为key，验证码为value，存入redis中，并设置超时时间为5分钟
        redisTemplate.opsForValue().set(emailAddress,verificationCode,5, TimeUnit.MINUTES);
        return R.success(null);
    }
}